package ninegag.storm.me.testloginmvp.presenter;

import android.text.TextUtils;
import android.widget.Toast;

import ninegag.storm.me.testloginmvp.model.IUserModel;
import ninegag.storm.me.testloginmvp.model.UserBean;
import ninegag.storm.me.testloginmvp.model.UserModel;
import ninegag.storm.me.testloginmvp.view.IUserView;
import ninegag.storm.me.testloginmvp.view.UserLoginActivity;

/**
 * Created by zxy on 2017/7/20.
 * presenter层的实现类  持有view层和model层的引用
 */
public class UserPresenter implements IUserPresenter {
    private IUserView mUserView;
    private IUserModel mUserModel;
    public UserPresenter(IUserView userView) {
        mUserView = userView;
        mUserModel = new UserModel();
    }
    @Override
    public void saveInfo() {
        if (TextUtils.isEmpty(mUserView.getUserName()) || TextUtils.isEmpty(mUserView.getUserPassword())){
            Toast.makeText((UserLoginActivity)mUserView, "用户名或者密码不能为空", Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText((UserLoginActivity)mUserView, "保存成功", Toast.LENGTH_SHORT).show();
            mUserModel.userSaveInfo(mUserView.getUserId(),mUserView.getUserName(),mUserView.getUserPassword());
        }
    }
    @Override
    public void loadInfo() {
        UserBean userInfo = mUserModel.getUserInfo(mUserView.getUserId());
        if (userInfo != null){
            mUserView.setUserName(userInfo.name);
            mUserView.setUserPassword(userInfo.password);
        }else {
            Toast.makeText((UserLoginActivity)mUserView, "该用户id不存在", Toast.LENGTH_SHORT).show();
        }
    }
}
